# -*- coding: utf-8 -*-
from django.db import models

from basic_info.models import Stock, Concept, Industry, Location

# 公司每日信息模型定义

class RumorVerification(models.Model):
    """
    传闻求证
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 发布个股
    stock = models.ForeignKey(Stock)
    # 传闻
    question = models.TextField()
    # 回答
    answer = models.TextField()
    # 原始链接
    url = models.URLField(max_length=200)
    # 是否为肯定回答
    is_positive = models.BooleanField()

    def __unicode__(self):
        return self.date, self.stock.short_name

    class Meta:
        unique_together = ('date', 'url')

class DailyMoneyFlow(models.Model):
    """
    当日资金流量
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 发布个股
    stock = models.ForeignKey(Stock)
    # 收盘价
    closed_price = models.FloatField()
    # 总流, 需排序
    total_flow = models.FloatField(db_index=True)
    # 超大宗流量
    super_flow = models.FloatField()
    # 大宗流量
    big_flow = models.FloatField()
    # 非大宗流量
    non_big_flow = models.FloatField()

    def __unicode__(self):
        return self.date, self.stock.short_name

    class Meta:
        unique_together = ('date', 'stock')

class DailyMoneyFlowByIndustry(models.Model):
    """
    当日按行业资金流量
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 行业
    industry = models.ForeignKey(Industry)
    # 总流量, 需排序
    total_flow = models.FloatField(db_index=True)

    def __unicode__(self):
        return self.date, self.industry.name

    @staticmethod
    def get_model():
        return Industry

    class Meta:
        unique_together = ('date', 'industry')

class DailyMoneyFlowByConcept(models.Model):
    """
    当日按概念资金流量
    """
    # 发布日期
    date = models.DateField(db_index=True)
    # 概念
    concept = models.ForeignKey(Concept)
    # 总流量, 需排序
    total_flow = models.FloatField(db_index=True)

    def __unicode__(self):
        return self.date, self.concept.name

    @staticmethod
    def get_model():
        return Concept

    class Meta:
        unique_together = ('date', 'concept')

class QuestionCategory:
    """
    问题所属主类类型
    """
    #重大事项
    SIGNIFICANT_EVENT = u'重大事项'
    #财务数据
    FINANCIAL_DATA = u'财务数据'
    #高管信息
    MANAGER_INFO = u'高管信息'
    #其他
    OTHERS = u'其他'

    MainCategory = (
                (SIGNIFICANT_EVENT, u'重大事项'),
                (FINANCIAL_DATA, u'财务数据'),
                (MANAGER_INFO, u'高管信息'),
                (OTHERS, u'其他'),
    )
    """
    问题所属子类类型
    """
    #并购重组
    MERGE_RESTRUCT = u'并购重组'
    #再融资
    REFINANCING = u'再融资'
    #诉讼
    LITIGATION = u'诉讼'
    #项目进展
    PROJECT_PROGRESS = u'项目进展'
    #股份变动
    STOCK_CHANGE = u'股份变动'
    #股权激励
    EQUITY_INCENTIVE = u'股权激励'
    #业绩
    ACHIEVEMENT = u'业绩'
    #财务指标
    FINANCIAL_INDICATOR = u'财务指标'
    #分红
    DIVIDEND = u'分红'
    #高管变动
    MANAGER_CHANGE = u'高管变动'
    #高管薪酬
    MANAGER_SALARY = u'高管薪酬'
    #高管持股
    MANAGER_OWNERSHIP = u'高管持股'

    SubCategory = (
               (MERGE_RESTRUCT, u'并购重组'),
               (REFINANCING, u'再融资'),
               (LITIGATION, u'诉讼'),
               (PROJECT_PROGRESS, u'项目进展'),
               (STOCK_CHANGE, u'股份变动'),
               (EQUITY_INCENTIVE, u'股权激励'),
               (ACHIEVEMENT, u'业绩'),
               (FINANCIAL_INDICATOR, u'财务指标'),
               (DIVIDEND, u'分红'),
               (MANAGER_CHANGE, u'高管变动'),
               (MANAGER_SALARY, u'高管薪酬'),
               (MANAGER_OWNERSHIP, u'高管持股'),
               (OTHERS, '其他'),

    )

class QuestionPolarity:

    # 问题情感倾向

    POSITIVE = 0
    NEGATIVE = 1
    NEUTRAL = 2
    UNCERTAIN = 3
    UNKNOWN = 4

    PolarityCategory = (
                    (POSITIVE, u'正面'),
                    (NEGATIVE, u'负面'),
                    (NEUTRAL, u'中立'), 
                    (UNCERTAIN, u'不缺定'),
                    (UNKNOWN, u'不知到'),
    )

class StatisticsCategory:
    
    #统计类型

    COMPANY = Stock.__name__
    INDUSTRY = Industry.__name__
    CONCEPT= Concept.__name__
    LOCATION = Location.__name__
    QUESTION = 'Question'

    Category = (
            (COMPANY, u'公司'),
            (INDUSTRY, u'行业'),
            (CONCEPT, u'概念'),
            (LOCATION, u'地域'),
            (QUESTION, u'问题类型'),
    )

class StatisticsCycle:
    
    #统计周期

    DAILY = 1
    WEEKLY = 7
    MONTHLY = 30

    Category = (
            (DAILY, 1),
            (WEEKLY, 7),
            (MONTHLY, 30),
    )

class StatisticsOrderBy:

    # 统计排序

    QUESTION = 'count'
    POSITIVE_RATE = 'positive_rate'
    NEGATIVE_RATE = 'negative_rate'

    OrderBy = (
               (QUESTION, u'问题'),
               (POSITIVE_RATE, u'情感个数'),
               (NEGATIVE_RATE, u'情感比率'), 
    )

class QA(models.Model):
    """
    互动问答
    """
    #提问日期
    question_date = models.DateTimeField()
    #回答日期
    answer_date = models.DateTimeField()
    #个股
    stock = models.ForeignKey(Stock)
    #问题
    question = models.CharField(max_length=200)
    #回答
    answer = models.TextField()
    #问题所属主类
    main_category = models.CharField(choices=QuestionCategory.MainCategory, max_length=10)
    #问题所属子类
    sub_category = models.CharField(choices=QuestionCategory.SubCategory, max_length=10)
    #问题的情绪类型
    polarity = models.IntegerField(choices=QuestionPolarity.PolarityCategory, default=3)

    class Meta:
        unique_together = ('question_date', 'stock', 'question')
        index_together = [
            ['answer_date', 'stock'],
            ['answer_date', 'stock', 'question', 'polarity'],
        ]

    def __unicode__(self):
        return self.question_date, self.stock.short_name, self.question

class QAStatisticsResult(models.Model):
    """
    互动问答统计结果
    """
    #统计的类型
    category = models.CharField(choices=StatisticsCategory.Category, max_length=10)
    #统计的名称
    name = models.CharField(max_length=20)
    #按名称统计的总条数
    count = models.IntegerField(default=0, db_index=True)
    #按名称统计的正面条数
    positive_count = models.IntegerField(default=0, db_index=True)
    #按名称统计的负面条数
    negative_count = models.IntegerField(default=0, db_index=True)
    #按名称统计的正面+负面总条数
    polarity_count = models.IntegerField(default=0, db_index=True)
    #按名称统计的正/总比值
    positive_rate = models.FloatField(default=0, db_index=True)
    #按名称统计的负/总比值
    negative_rate = models.FloatField(default=0, db_index=True)
    #统计的日期
    date = models.DateField()
    #统计的周期
    cycle = models.IntegerField(choices=StatisticsCycle.Category)

    def __unicode__(self):
        return self.category+self.name+self.count
    class Meta:
        unique_together = ('category', 'name', 'date', 'cycle')
        index_together = [
            ['category', 'name', 'date', 'cycle']
        ]
